<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="../js/cookie.js"></script>
    <title>Document</title>
</head>
<body>
    <div class="box">
        <p>
            <label for="">用户名:</label>
            <input type="text" class="user" placeholder="用户名/密码/邮箱">
        </p>
        <p>
            <label for="">密&emsp;码:</label>
            <input type="text" class="pwd" placeholder="密码">
        </p>
        <p>
            <label>
                <input type="checkbox" class="isRem">是否记住本账号?
            </label>
        </p>
        <p>
            <input class="btn" type="submit" value="登录">
        </p>
    </div>
</body>
<script>
    var userInp = document.getElementsByClassName("user")[0];
    var pwdInp = document.getElementsByClassName("pwd")[0];
    var btn = document.getElementsByClassName("btn")[0];
    var isRem = document.getElementsByClassName("isRem")[0];
  
    btn.onclick = function() {
        
    // 用户输入的
        var account = userInp.value;
        var pwd = pwdInp.value;


    // 数据库存储的

        if(account && pwd) {
            var xhr = new XMLHttpRequest();
            xhr.open("post","../php/login.php", true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
            xhr.send(`account=${account}&pwd=${pwd}`);
            xhr.onreadystatechange = function() {
                if(xhr.readyState == 4 && xhr.status == 200) {
                    console.log(xhr.responseText);
                    var result = JSON.parse(xhr.responseText);
                    console.log(result);
                    if(result.status) {
                        if(isRem.checked) {
                            setCookie("lgc",result.user,7);
                        } else {
                            setCookie("lgc",result.user);
                        }
                        alert("登录成功");
                        location.href = "./index.html"
                    } else {
                        alert(result.msg);
                    }
                } 
            }
        } 
    }





</script>
</html>